package com.clz.hyperit.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.clz.hyperit.dao.QuestionTagDao;
import com.clz.hyperit.entity.QuestionTag;
import com.clz.hyperit.vo.PageInfo;

@Repository("questionTagDao")
public class QuestionTagDaoImpl extends BaseDaoImpl<QuestionTag> implements
		QuestionTagDao {

	@SuppressWarnings("unchecked")
	public List<QuestionTag> findByQuestionId(Integer id) {
		String hql = "from QuestionTag where question_id = :id";
		return getCurrentSession().createQuery(hql).setInteger("id", id).list();
	}

	@SuppressWarnings("unchecked")
	public List<QuestionTag> getByQuestionId(Integer tag_id, PageInfo pageInfo) {
		String hql = " from QuestionTag where tag_id = :tag_id order by createtime desc";
		List<QuestionTag> list = getCurrentSession()
				.createQuery(hql)
				.setInteger("tag_id", tag_id)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from QuestionTag where tag_id = :tag_id ";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.setInteger("tag_id", tag_id)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
	}

}
